Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

143 - experiment - improved jsdoc and exported to a type definition file #149

Closed
wants to merge 1 commit into from

Conversation

isaacHagoel
Copy link
Owner

This is just a quick experiment.
Running yarn build generates index.d.ts in the dist folder with what seems to be reasonable type definitions.
I tried to use it within a svelte typescript project (with npm link to a local copy) and it doesn't seem to acknowledge the type definitions or do anything useful, at least with VSCode. Maybe it is because of npm link or the fact that svelte consumes the code directly from src. I am not sure.
Within Intellij, the type definitions seem to be picked up and working properly within .ts files (see image) but proper tooling is missing for .svelte files so I can't check how it behaves in context.
image

There is a TODO item regarding typing the emitted event (finalize and consider which basically have the same shape)

@isaacHagoel isaacHagoel mentioned this pull request Oct 14, 2020
@isaacHagoel
Copy link
Owner Author

Actually, with a normal .ts file it works well in VSCode as well. It is only problematic within the .svelte file for some reason.
image

@isaacHagoel
Copy link
Owner Author

isaacHagoel commented Oct 14, 2020

So I continued down the rabbit hole and it turns out custom events are not supported out of the box.
there is an open issue for it and a potential workaround (global declaration within the Svelte.jsx namesapce, see issue).
I used a very silly example to see how vscode + svelte deal with typed actions and encountered this issue.

my silly example App.svelte

<script lang="ts">
	import {act} from './act';
</script>
<section use:act={{a: "5"}} on:test={(e) => {}}>
	Hello
</section>

act.ts

type Opts = {
  a: string
}
export function act(node: HTMLElement, options: Opts) {
  console.log(node);
  node.dispatchEvent(new CustomEvent('test', { detail: {} }));
  return {
    update: (newoptions: Opts) => { },
    destroy: () => { } 
  }
}

The method suggested in the ticket (global.d.ts) makes the error on 'ontest' go way but I couldn't make it understand the type that handler is supposed to enforce (it always thinks it is of type 'any' not matter how I tweak the declaration).
This is not great to say the least.

@isaacHagoel
Copy link
Owner Author

More findings:
turns out the vscode plugin has a really strange behaviour. When importing svelte-dnd-action in the root component it can't find the types but when importing from any other component it can.
In order for it not to complain about on:condsider and on:finalise and in order to have type info about the event, I have added this to index.d.ts for svelte-dnd-action:

declare enum TRIGGERS {
    DRAG_STARTED,
    DRAGGED_ENTERED,
    DRAGGED_OVER_INDEX,
    DRAGGED_LEFT_EVENT_NAME,
    DROPPED_INTO_ZONE,
    DROPPED_INTO_ANOTHER,
    DROPPED_OUTSIDE_OF_ANY,
    DRAG_STOPPED
}
declare enum SOURCES {
    POINTER,
    KEYBOARD
}

type Info = {
    trigger: TRIGGERS;
    id: string;
    source: SOURCES;
};
type DndEvent = {
    detail: {
        items: Array<object>,
        info: Info
    }
}

and exported DndEvent.
I couldn't find a way to make JSDoc generate exactly these type definitions automatically (ex: make it create an actual enum, even with the @enum annotation). Looks like if we don't go with full type integration, it is best to create the definitions file manually.

In the Svelte app I had to add a global.d.ts file that looks as follows (will need to be added to the README if we go forward with typescript):

declare type DndEvent = { detail: import('svelte-dnd-action').DndEvent };
declare namespace svelte.JSX {
    interface HTMLAttributes<T> {
        onconsider?: function(DndEvent): void
        onfinalize?: (e: DndEvent) => void;
    }
}

tsconfig.json needs to be edited to include it "include": ["src/**/*", "global.d.ts"], (when i've put it in /src vscode couldn't see it).
Notice: No matter what I tried, within the svelte component it always treats e as type any, therefore I explicitly added the type to the handler function in the component that consumes the library:

function handleSort(e: DndEvent) {
        items = e.detail.items;
}

Otherwise, it seems to work normally.

@isaacHagoel isaacHagoel deleted the 143-jsdoc-to-types branch October 31, 2020 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant